package com.gobang.game;

import com.gobang.config.WebSocketConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.net.http.WebSocket;
import java.util.concurrent.ConcurrentHashMap;

//用于管理当前用户的游戏大厅和游戏房间的在线状态
@Component
public class OnlineUserManager {
    //哈希表表示当前用户在游戏大厅发的在线状态,key是Id，value是session里面的用户信息
    //线程安全，ConcurrentHashMap是每一个哈希桶进行上锁，多用户断开/建立连接更加安全
    private ConcurrentHashMap<Integer,WebSocketSession> gameHall = new ConcurrentHashMap<>();
    //key是Id，value是session里面的用户信息
    private ConcurrentHashMap<Integer,WebSocketSession> gameRoom = new ConcurrentHashMap<>();
    //GamHall,游戏大厅的登入登出以及查询
    public void enterGanmeHall(int userId, WebSocketSession session){
        gameHall.put(userId,session);
    }
    //退出游戏大厅
    public void exitGameHall(int userId){
        gameHall.remove(userId);
    }

     public WebSocketSession getFromGameHall(int userId){
        return gameHall.get(userId);
     }
     //Room,游戏房间的登入登出以及查询
     public WebSocketSession getFromGameRoom(int userId){
        return gameRoom.get(userId);
     }

     public void enterGameRoom(int userId,WebSocketSession session){
        gameRoom.put(userId,session);
    }
    public void exitGameRoom(int userId){
        gameRoom.remove(userId);
    }


}
